//
//  FGGoodsTemplateView.m
//  lite
//
//  Created by feige on 2022/11/2.
//

#import "FGGoodsTemplateView.h"
#import "UIView+FGLayout.h"
#import "FGResources.h"
#import <SDWebImage/SDWebImage.h>

@interface FGGoodsTemplateView()
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *subtitleLabel;
@property (nonatomic, strong) UILabel *amountLabel;
@property (nonatomic, strong) id templateData;
@end

@implementation FGGoodsTemplateView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        // imageView
        _imageView = [UIImageView new];
        _imageView.clipsToBounds = YES;
        _imageView.contentMode = UIViewContentModeScaleAspectFill;
        [self addSubview:_imageView];
        
        // titleLabel
        _titleLabel = [UILabel new];
        _titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
        [self addSubview:_titleLabel];
        
        // subtitleLabel
        _subtitleLabel = [UILabel new];
        _subtitleLabel.numberOfLines = 2;
        _subtitleLabel.font = [UIFont systemFontOfSize:14];
        _subtitleLabel.textColor = [UIColor lightGrayColor];
        [self addSubview:_subtitleLabel];
        
        // amountLabel
        _amountLabel = [UILabel new];
        _amountLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
        [self addSubview:_amountLabel];
        
        _imageView.image = [FGResources getImageFromMessage:@"placeholder_image"];
        _titleLabel.text = @"产品标题1";
        _subtitleLabel.text = @"描述信息描述信息描述信息描述信息…";
        _amountLabel.text = @"¥88.88";
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    _imageView.mm_top(10).mm_left(10).mm_width(78).mm_height(90);
    
    _titleLabel.frame = CGRectMake(99, 10, self.mm_w - 109, 22);
    
    _subtitleLabel.mm_sizeToFit().mm_left(_titleLabel.mm_x).mm_width(self.mm_w - 109).mm_top(_titleLabel.mm_maxY + 5);
    
    _amountLabel.mm_sizeToFit().mm_left(_titleLabel.mm_x).mm_top(82);
}

#pragma mark - Rewrite Mehtod

- (void)fillData:(id)templateData {
    self.templateData = templateData;
    
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:[self.templateData valueForKeyPath:@"data.imageUrl"]] placeholderImage:[FGResources getImageFromMessage:@"placeholder_image"]];
    self.titleLabel.text = [self.templateData valueForKeyPath:@"data.title"];
    self.subtitleLabel.text = [self.templateData valueForKeyPath:@"data.description"];
    self.amountLabel.text = [NSString stringWithFormat:@"¥%@", [self.templateData valueForKeyPath:@"data.price"]];
}

/*
 {
     "isView":true,
     "id":44,
     "data":{
         "linkUrl":"https:\/\/www.baidu.com\/s?tn=68018901_40_oem_dg&ie=utf-8&wd=%E7%BE%8E%E5%A5%B3",
         "id":44,
         "price":"你好",
         "title":"美女",
         "imageUrl":"https:\/\/testfebookpic.oss-cn-hangzhou.aliyuncs.com\/febookimg\/202210\/3_93be3fec7bd5425389ff689c4756c9a1.jpg",
         "updateTime":"2022-10-27 17:05:10",
         "description":"只要998",
         "websiteId":1,
         "createTime":"2022-10-26 19:02:45",
         "companyId":1
     },
     "children":null,
     "type":3,
     "tips":null,
     "name":"美女",
     "pid":3
}
 */

@end
